﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.WebUI
{
	/// <summary>
	/// 调查列表。
	/// </summary>
	public class VoteList : BaseRepeaterList
	{
		private LoadedValue<string> loadedClassCode;
		/// <summary>
		/// 列表的类别代码。
		/// </summary>
		public virtual string ClassCode
		{
			get
			{
				if (loadedClassCode.Loader == null)
					loadedClassCode.Loader = () => (string)ViewState["ClassCode"];
				return loadedClassCode.Value;
			}
			set
			{
				if (loadedClassCode.Setter == null)
					loadedClassCode.Setter = v => ViewState["ClassCode"] = v;
				loadedClassCode.Value = value;
				loadedVoteClassInfo.ResetValue();
			}
		}

		private LoadedValue<int?> loadedChannelId;
		public virtual  int? ChannelId
		{
			get
			{
				if (loadedChannelId.Loader == null)
					loadedChannelId.Loader = () => (int?)ViewState["ChannelId"];
				return loadedChannelId.Value;
			}
			set
			{
				if (loadedChannelId.Setter == null)
					loadedChannelId.Setter = v => ViewState["ChannelId"] = v;
				loadedChannelId.Value = value;
			}
		}

		protected LoadedValue<VoteClass> loadedVoteClassInfo;
		public VoteClass VoteClassInfo
		{
			get
			{
				if (loadedVoteClassInfo.Loader == null)
					loadedVoteClassInfo.Loader = () => SiteId.HasValue ? VoteClasses.GetItem(SiteId.Value, ClassCode) : null;
				return loadedVoteClassInfo.Value;
			}
		}

		private LoadedValue<string> loadedKeyword;
		/// <summary>
		/// 调查信息的关键字。
		/// </summary>
		public virtual string Keyword
		{
			get
			{
				if (loadedKeyword.Loader == null)
					loadedKeyword.Loader = () => (string)ViewState["Keyword"];
				return loadedKeyword.Value;
			}
			set
			{
				if (loadedKeyword.Setter == null)
					loadedKeyword.Setter = v => ViewState["Keyword"] = v;
				loadedKeyword.Value = value;
			}
		}

		private LoadedValue<int?> loadedClassId;
		/// <summary>
		/// 列表的类别ID。
		/// </summary>
		public int? ClassId
		{
			get
			{
				if (loadedClassId.Loader == null)
					loadedClassId.Loader = () => SiteId.HasValue ? VoteClasses.GetIdentifier(SiteId.Value, ClassCode) : null;
				return loadedClassId.Value;
			}
		}

		private LoadedValue<string> loadedListTitle;
		/// <summary>
		/// 列表的标题，如不进行设置，则使用模块的名称。
		/// </summary>
		public override string ListTitle
		{
			get
			{
				if (loadedListTitle.Loader == null)
					loadedListTitle.Loader = () => (string)ViewState["ListTitle"];
				return string.IsNullOrEmpty(loadedListTitle.Value) ? VoteClasses.GetNameById(ClassId.Value) : loadedListTitle.Value;
			}
			set
			{
				if (loadedListTitle.Setter == null)
					loadedListTitle.Setter = v => ViewState["ListTitle"] = v;
				loadedListTitle.Value = value;
			}
		}



		/// <summary>
		/// 列表更多 Url。
		/// </summary>
		public override string ListMoreUrl
		{
			get
			{
				return GenericUrlManager.BuildVoteListUrl(ClassCode, null);
			}
		}

		/// <summary>
		/// 列表标题字段。
		/// </summary>
		public override string TitleField
		{
			get { return "Title"; }
		}

		/// <summary>
		/// 列表日期字段。
		/// </summary>
		public override string DateTimeField
		{
			get { return "ReleaseDate"; }
		}

		/// <summary>
		/// 列表项Url。
		/// </summary>
		public override string ItemUrl
		{
			get { return GenericUrlManager.BuildVoteDetailUrl(ClassCode, (int)Eval("VoteId")); }
		}

		/// <summary>
		/// 获取列表数据。
		/// </summary>
		/// <returns>列表数据。</returns>
		public override object GetListData()
		{
			return Votes.GetBaseList(ClassId, Keyword, LoadCount);
		}
	}
}